<?php
namespace app\servercertificate\logic;
use \app\manage\logic\BaseLogic;
use \think\Db;
/**
 * @author:      李颖
 * @DateTime:    2017-11-07 15:42:53
 * @Description: 证书发票
 */
class Invoice extends BaseLogic
{
	protected $name = 'invoice';
    protected $config = 'db_config_certificate';
    protected $invoiceModel = 'app\servercertificate\model\Invoice';
    protected $pickupgoodsModel = 'app\servercertificate\model\Pickupgoods';

    /*我的提货单申请发票*/
    public function applyInvoice($data): array 
    {
    	$_where['pick_up_id'] = $data['pick_up_id'];
    	$_where['user_id'] = $data['user_id'];
    	$_where['status'] = 1;
    	$info = $this->model($this->invoiceModel)->where($_where)->find();
    	if($info){
    		return verify_err($info, '您已申请过发票信息');
    	}
    	$certificate_id = $this->model($this->pickupgoodsModel)->where(['id'=>$data['pick_up_id']])->value('certificate_id');
    	$data['certificate_id'] = $certificate_id;
    	$res = $this->model($this->invoiceModel)->save($data);
    	return auto_return($res, '申请发票成功', '申请发票失败');
    }

    /*后台获取申请发票列表*/
    public function invoiceList($data): array 
    {
    	$_where['i.status'] = 1;
    	$list = $this->model($this->invoiceModel)
    				->alias('i')
    				->field('i.*,p.pick_up_code,p.name as pick_up_name,p.phone as pick_up_phone,p.address as pick_up_address,c.name as certificate_name')
    				->join('pick_up_goods p', 'p.id = i.pick_up_id', 'LEFT')
    				->join('certificate c', 'i.certificate_id = c.id', 'LEFT')
    				->where($_where)
                    ->order('i.id desc')
    				->paginate(null, false, ['page'=>$data['page'],'list_rows'=>$data['listRows']])
    				->toArray();
    	return auto_return_page($list, '成功获取发票列表', '获取发票列表失败');
    }

}